package com.yhb.myshop.web.admin.service.impl;

import com.yhb.myshop.domain.TbCategory;
import com.yhb.myshop.web.admin.dao.TbCategoryDao;
import com.yhb.myshop.web.admin.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl extends BaseServiceImpl<TbCategory, TbCategoryDao> implements CategoryService {

    @Override
    public List<TbCategory> getAll() {
        List<TbCategory> source = dao.getAll();
        List<TbCategory> sortedList = new ArrayList<>();
        sort(source,sortedList,0L);
        return sortedList;
    }

    private void sort(List<TbCategory> source,List<TbCategory> dest,Long parentId) {

        for (TbCategory category : source) {
            if(category.getParentId()==parentId){
                dest.add(category);
                if(category.isParent()){
                    sort(source,dest,category.getId());
                }
            }

        }
    }
}
